//package com.woniuxy.commons.consumer;
//
//import cn.hutool.json.JSONUtil;
//import com.alipay.api.AlipayClient;
//import com.alipay.api.request.AlipayTradeRefundRequest;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.woniuxy.elderlycare.dao.GoodsMapper;
//import com.woniuxy.elderlycare.dao.GoodsOrderInfoMapper;
//import com.woniuxy.elderlycare.dao.GoodsOrderItemMapper;
//import com.woniuxy.elderlycare.dao.SqtkMapper;
//import com.woniuxy.elderlycare.model.GoodsOrderInfo;
//import com.woniuxy.elderlycare.model.Sqtk;
//import com.woniuxy.elderlycare.util.AlipayUtil;
//import com.woniuxy.elderlycare.util.StateUtil;
//import org.springframework.amqp.rabbit.annotation.RabbitHandler;
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import java.math.BigDecimal;
//import java.util.UUID;
//
//@Component
//@RabbitListener(queues = "order_tk_queue")
//public class TkConsume {
//    @Autowired
//    private AlipayUtil alipayUtil;
//    @Autowired
//    private GoodsOrderInfoMapper goodsOrderInfoMapper;
//    @Autowired
//    private GoodsOrderItemMapper goodsOrderItemMapper;
//    @Autowired
//    private GoodsMapper goodsMapper;
//    @Autowired
//    private SqtkMapper sqtkMapper;
//    @Autowired
//    private AlipayClient alipayClient;
//
//    @RabbitHandler
//    public void handler(String orderNum) throws Exception {
////设置请求参数
//        AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();
//        //商户订单号，商户网站订单系统中唯一订单号
//        String out_trade_no = orderNum;
//        GoodsOrderInfo orderInfo = goodsOrderInfoMapper.selectById(out_trade_no);
//        QueryWrapper<Sqtk> wrapper = new QueryWrapper<>();
//        wrapper.eq("order_id",orderNum);
//        Sqtk sqtk = sqtkMapper.selectOne(wrapper);
////支付宝交易号
////        String trade_no = new String(request.getParameter("WIDTRtrade_no").getBytes("ISO-8859-1"),"UTF-8");
////请二选一设置
////需要退款的金额，该金额不能大于订单金额，必填
//        BigDecimal refund_amount = orderInfo.getMoney();
////退款的原因说明
//        String refund_reason = sqtk.getReason();
////标识一次退款请求，同一笔交易分多次退款的话，out_request_no每次传值都不能重复，必须保证唯一性
////        String out_request_no = new String(request.getParameter("WIDTRout_request_no").getBytes("ISO-8859-1"),"UTF-8");
//
//        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
//                + "\"refund_amount\":\""+ refund_amount +"\","
//                + "\"refund_reason\":\""+ refund_reason +"\","
//                + "\"out_request_no\":\""+ UUID.randomUUID().toString().replace("-","") +"\"}");
//
////请求
//        String result = alipayClient.execute(alipayRequest).getBody();
//        String code = JSONUtil.parseObj(JSONUtil.parseObj(result).get("alipay_trade_refund_response").toString()).get("code").toString();
//        String fundChange = JSONUtil.parseObj(JSONUtil.parseObj(result).get("alipay_trade_refund_response").toString()).get("fund_change").toString();
//        if (!(code.equals("10000")&&fundChange.equals("Y"))) throw new Exception("退款错误");
//        orderInfo.setOrderState(StateUtil.ORDER_INFO_TUIKUAN);
////        QueryWrapper<GoodsOrderItem> w = new QueryWrapper<>();
////        w.eq("order_info_id",orderInfo.getId());
////        GoodsOrderItem orderItem = goodsOrderItemMapper.selectOne(w);
////        Goods goods = goodsMapper.selectById(orderItem);
////        goods.setGoodsNum(goods.getGoodsNum()+orderItem.getGoodsCont());
////        goodsMapper.updateById(goods);
//        goodsOrderInfoMapper.updateById(orderInfo);
//
//
//    }
//}
